home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Processes / LaunchWithDoc / LaunchWithDoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.9 KB  |  117 lines  |  [TEXT/MPS ]

  1. /* LaunchWithDoc */
  2. /* The smallest LaunchAplication example I could come up with. */
  3. /* this launches an application with a 'odoc' AppleEvent™ */
  4. /* this little bit of code here uses two Standard File calls to get */
  5. /* the application to launch and the file to launch with, you will of course */
  6. /* want to replace these with whatever you want to do */
  7. /* ••• NOTE: This also works for launching non-System 7 applications.  */
  8. /* If the Finder™ sees an 'odoc' or 'pdoc' Apple event in the launch block */
  9. /* and the application being launched is NOT system 7 aware, the Finder */
  10. /* coerces the Apple event into puppetstrings.  So you */
  11. /* don't need to special case for non-7.0 applications.  */
  12. /* Pretty neat, huh? */
  13. /* ••• Important: I'm not doing any error checking, you of course should */
  14. /* so your users don't hunt you down and burn your keyboard. */
  15. /* C.K. Haun */
  16. /* Apple DTS */
  17.  
  18. /* And another Note: */
  19. /* Please see the sample FinderOpenSel 1.0.1 for more information on opening/launching */
  20. /* applications and stuff.  Using the Finder event is much more versitile than */
  21. /* just using launchApplication. */
  22. #include <Dialogs.h>
  23. #include <QuickDraw.h>
  24. #include <Windows.h>
  25. #include <Menus.h>
  26. #include <Fonts.h>
  27. #include <appleevents.h>
  28. #include <processes.h>
  29. #include <files.h>
  30. #include <StandardFile.h>
  31. #include <Aliases.h>
  32. main()
  33. {
  34.     FSSpec launchApp, fileToLaunch;   /* file spec for the app (launchApp) and the file (fileToLaunch) */
  35.     OSErr myErr;
  36.     LaunchParamBlockRec launchThis;
  37.     AEDesc myAddress;
  38.     ProcessSerialNumber myPSN;
  39.     AEDesc docDesc, launchDesc;
  40.     AEDescList theList;
  41.     AliasHandle withThis;
  42.     StandardFileReply myReply;
  43.     AppleEvent theEvent;
  44.     InitGraf((Ptr)&qd.thePort);  /* standard setup stuff */
  45.     InitFonts();
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(nil);
  50.     InitCursor();
  51.     /* get the app to launch, using the Sys7 versions of Standard File */
  52.     /* which return FSSpecs */
  53.     /* Preset our address descriptor to our PSN */
  54.     /* The Finder is going to change this to whatever is appropriate  */
  55.     /* for the application that eventually gets launched, so I'll */
  56.     /* just fill it with my own address. */
  57.     /* I'm doing this because I have heard sporadic reports that  */
  58.     /* _not_ doing this causes address errors sometimes, I've never had that happen. */
  59.     /* But since it has, you should prefill. */
  60.     GetCurrentProcess(&myPSN);
  61.     /* create the address desc for the event */
  62.     myErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&myPSN, sizeof(ProcessSerialNumber), &myAddress);
  63.     /* get the application to launch */
  64.     StandardGetFile(nil, -1, nil, &myReply);
  65.     if (!myReply.sfGood)
  66.         return;
  67.     launchApp = myReply.sfFile;
  68.     /* stuff it in my launch parameter block */
  69.     launchThis.launchAppSpec = &launchApp;
  70.     
  71.     /* get the file to pass */
  72.     StandardGetFile(nil, -1, nil, &myReply);
  73.     if (!myReply.sfGood)
  74.         return;
  75.     
  76.     /* the caller may have already done this, but it doesn't hurt to do it again */
  77.     fileToLaunch = myReply.sfFile;
  78.     /* create an appleevent to carry it */
  79.     AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &myAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  80.     /* create a list for the alaises.  In this case, I only have one, but you still need */
  81.     /* a list */
  82.     AECreateList(nil, 0, false, &theList);
  83.     /* create an alias out of the file spec */
  84.     /* I'm not real sure why I did this, since there is a system coercion handler for */
  85.     /* alias to FSSpec, but I'm paranoid */
  86.     NewAlias(nil, &fileToLaunch, &withThis);
  87.     HLock((Handle)withThis);
  88.     /* now create an alias descriptor */
  89.     AECreateDesc(typeAlias, (Ptr)*withThis, GetHandleSize((Handle)withThis), &docDesc);
  90.  
  91.     HUnlock((Handle)withThis);
  92.     /* put it in the list */
  93.     AEPutDesc(&theList, 0, &docDesc);
  94.     AEPutParamDesc(&theEvent, keyDirectObject, &theList);
  95.     /* coerce the event from being an event into being appParms */
  96.     /* •••• If you just want to send an 'odoc' to an application that is */
  97.     /* already running, you can stop here and do an AESend on theEvent */
  98.     AECoerceDesc(&theEvent, typeAppParameters, &launchDesc);
  99.     HLock((Handle)theEvent.dataHandle);
  100.     /* and stuff it in the parameter block */
  101.     /* This is a little weird, since we're actually moving the event out of the */
  102.     /* AppParameters descriptor.  But it's necessary, the coercison to typeAppParameters */
  103.     /* stuffs the whole appleevent into one AERecord (instead of a AEDesc) so  */
  104.     /* the Finder gets the whole event as one handle.  It can then parse it itself */
  105.     /* to do the sending */
  106.     launchThis.launchAppParameters = (AppParametersPtr)*(launchDesc.dataHandle);
  107.     /* launch the thing */
  108.     launchThis.launchBlockID = extendedBlock;
  109.     launchThis.launchEPBLength = extendedBlockLen;
  110.     launchThis.launchFileFlags = nil;
  111.     launchThis.launchControlFlags = launchContinue + launchNoFileFlags;
  112.     LaunchApplication(&launchThis);
  113.     /* and launch it */
  114.     
  115.     
  116. }
  117.